package org.example.utils.redis;

import org.example.constans.EssenceConstants;
import org.example.utils.RedisPoolUtils;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Component
public class ServerRedisFactory implements ApplicationContextAware {

    private static Map<String, RedisPoolUtils> redisPoolUtilsMap = new HashMap<>();


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RedisProperties bean = applicationContext.getBean(RedisProperties.class);
        redisPoolUtilsMap.put(EssenceConstants.DEFAULT_APP_ID, new RedisPoolUtils(bean));
    }

    public static RedisPoolUtils getRedisPoolUtils(){
        return redisPoolUtilsMap.get(EssenceConstants.DEFAULT_APP_ID);
    }



    public static RedisPoolUtils getRedisPoolUtilsByAppId(String appId){
        RedisPoolUtils redisPoolUtils;
        if(appId==null || ( redisPoolUtils = redisPoolUtilsMap.get(appId)) == null){
            return getRedisPoolUtils();
        }
        return redisPoolUtils;
    }

}
